from pydantic_settings import BaseSettings
from typing import Optional
import os
import secrets

class Settings(BaseSettings):
    """
    应用配置类
    """
    # API配置
    API_V1_STR: str = "/api/v1"
    PROJECT_NAME: str = "混元智能体"
    
    # 安全配置
    SECRET_KEY: str = secrets.token_urlsafe(32)
    ACCESS_TOKEN_EXPIRE_MINUTES: int = 60 * 24 * 8  # 8 天
    
    # 数据库配置
    SQLITE_URL: str = "sqlite:///./hunyuan.db"
    DATABASE_URL: str = os.getenv(
        "DATABASE_URL", 
        SQLITE_URL
    )
    
    # PostgreSQL配置
    POSTGRES_SERVER: Optional[str] = None
    POSTGRES_USER: Optional[str] = None
    POSTGRES_PASSWORD: Optional[str] = None
    POSTGRES_DB: Optional[str] = None
    
    # DeepSeek API配置
    DEEPSEEK_API_KEY: Optional[str] = None
    DEEPSEEK_API_BASE: str = "https://api.deepseek.com/v1"
    
    # 应用配置
    APP_NAME: str = "混元智能体"
    DEBUG: bool = True
    
    @property
    def SQLALCHEMY_DATABASE_URL(self) -> str:
        """
        获取数据库URL
        如果设置了PostgreSQL配置，则使用PostgreSQL，否则使用SQLite
        """
        if self.POSTGRES_SERVER:
            return (
                f"postgresql://{self.POSTGRES_USER}:{self.POSTGRES_PASSWORD}"
                f"@{self.POSTGRES_SERVER}/{self.POSTGRES_DB}"
            )
        return self.DATABASE_URL
    
    class Config:
        case_sensitive = True
        env_file = ".env"

# 创建全局设置实例
settings = Settings() 